package com.syqc.videos

import android.view.View
import androidx.activity.viewModels
import androidx.core.view.isGone
import androidx.core.view.isVisible
import autodispose2.autoDispose
import com.shuyu.gsyvideoplayer.listener.GSYSampleCallBack
import com.syqc.comlib.base.BaseActivity
import com.syqc.entity.HistoryVideo
import com.syqc.monitor.databinding.ActivityTmnPlayerBinding
import com.syqc.utils.Keys
import com.syqc.utils.UrlUtil
import com.syqc.videos.videomanager.TapeManager

class TmnPlayerActivity :
    BaseActivity<ActivityTmnPlayerBinding>(ActivityTmnPlayerBinding::inflate) {
    private val mViewModel: TmnPlayerVM by viewModels()


    override fun initView() {
        val result: HistoryVideo.Result =
            intent.getSerializableExtra(Keys.INTENT_DATA) as HistoryVideo.Result
        mViewModel.carId = intent.getStringExtra(Keys.INTENT_CAR_ID) ?: ""
        mViewModel.handleEntity(result)
        initPlayer()
        initVM()
    }

    override fun setListener() {

    }

    private fun initPlayer() {
        val title = "${intent.getStringExtra(Keys.INTENT_TITLE)}--${mViewModel.getName()} "
        binding.tmnPlayer.setTitle(title)
        binding.tmnPlayer.setSeekBarState(mViewModel.maxTime)

        binding.tmnPlayer.setVideoAllCallBack(object : GSYSampleCallBack() {
            override fun onPlayError(url: String?, vararg objects: Any?) {
                super.onPlayError(url, *objects)
                releasePlayer()
                mViewModel.getErrorState()
            }

            override fun onPrepared(url: String?, vararg objects: Any?) {
                super.onPrepared(url, *objects)
                binding.tmnPlayer.startTime()
            }
        })

        binding.tmnPlayer.setTmnListener(object : com.syqc.videos.interfaces.ITmnListener {
            override fun onProgress(progress: Long) {
                if (progress >= mViewModel.maxTime) {
                    releasePlayer()
                }
            }

            override fun onSeekProgress(progress: Long) {
                if (progress >= mViewModel.maxTime) {
                    releasePlayer()
                } else {
                    releasePlayer()
                    mViewModel.setSeekTime(progress)
                    binding.tmnPlayer.setSeekBarProgress(progress, mViewModel.maxTime)
                    binding.tmnPlayer.setUp(
                        mViewModel.getUrlTime(mViewModel.seekToTime),
                        false,
                        title
                    )
                    binding.tmnPlayer.startPlayLogic()
                }
            }

            override fun onMultipleListener(multiple: Int) {
                mViewModel.multipleVideo(multiple)
            }

            override fun onPlayState(type: com.syqc.videos.interfaces.PlayState) {
                when (type) {
                    com.syqc.videos.interfaces.PlayState.PAUSE -> mViewModel.pauseVideo()
                    com.syqc.videos.interfaces.PlayState.FAST -> mViewModel.fastVideo()
                    com.syqc.videos.interfaces.PlayState.BACK -> mViewModel.backVideo()
                    com.syqc.videos.interfaces.PlayState.FRAME -> mViewModel.frameVideo()
                }
            }

            override fun onBackListener() {
                destroy()
                finish()
            }

            override fun onPlaying(isPlaying: Boolean) {
                if (isPlaying) {
                    binding.tmnPlayer.showProgress(false)
                    mViewModel.pauseVideo()
                } else {
                    if (mViewModel.videoType == 0) {
                        binding.tmnPlayer.setUp(mViewModel.getUrlTime(), false, title)
                        binding.tvTmnToast.isGone = true
                        binding.tmnPlayer.showProgress(false)
                        binding.tmnPlayer.startPlayLogic()
                    } else {
                        mViewModel.resumeVideo()
                    }
                }
            }
        })
    }

    private fun initVM() {
        mViewModel.toastObserver().autoDispose(scopeProvider).subscribe(this::toastObserver)

        mViewModel.videoToastLive.observe(this) {
            binding.tmnPlayer.showProgress(false)
            binding.tvTmnToast.text = UrlUtil.getToastMap()[it ?: "88"]
            binding.tmnPlayer.release()
            binding.tvTmnToast.isVisible = true
            binding.tmnPlayer.showStartPlay(true)
        }

        mViewModel.videoStateLive.observe(this) {
            when (it) {
                //暂停
                1 -> {
                    binding.tmnPlayer.setPlayState(false)
                    binding.tmnPlayer.gsyVideoManager.player.pause()
                }
                //快进\快退
                2, 3 -> {
                    binding.tmnPlayer.setPlayState(false)
                    binding.tmnPlayer.isShowMultiple(true)
                }

                //关键帧播放
                4 -> {
                    binding.tmnPlayer.setPlayState(false)
                }
                //恢复播放
                5 -> {
                    binding.tmnPlayer.gsyVideoManager.player.start()
                    binding.tmnPlayer.setPlayState(true)
                    binding.tmnPlayer.isShowMultiple(false)
                    binding.tmnPlayer.setMultiple(1, true)
                    mViewModel.multiple = 1
                }
                //拖动播放 、关闭
                else -> {}
            }
        }
    }

    /**
     * 重置播放
     *
     */
    private fun releasePlayer() {
        mViewModel.stopPlay()
        binding.tmnPlayer.run {
            release()
            onVideoReset()
            setSeekBarState(mViewModel.maxTime)
            setPlayState(true)
            setMultiple(1, true)
            showStartPlay(true)
        }
        TapeManager.instance.releaseMediaPlayer()
    }

    /**
     * 销毁
     */
    private fun destroy() {
        mViewModel.stopPlay()
        binding.tmnPlayer.stopTime()
        binding.root.postDelayed({
            binding.tmnPlayer.release()
            TapeManager.instance.releaseMediaPlayer()
        }, 100)
    }

    override fun onDestroy() {
        super.onDestroy()
        destroy()
    }

    override fun onPause() {
        super.onPause()
        binding.tmnPlayer.onVideoPause()
    }

    override fun onResume() {
        super.onResume()
        binding.tmnPlayer.onVideoResume()
    }

    override fun onWindowFocusChanged(hasFocus: Boolean) {
        super.onWindowFocusChanged(hasFocus)
        window.decorView.systemUiVisibility =
            View.SYSTEM_UI_FLAG_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or
                    View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
    }
}